﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using InstallManager.Msi;
using System.Collections.ObjectModel;

namespace InstallerGUI
{
    /// <summary>
    /// Interaction logic for InstallControl.xaml
    /// </summary>
    public partial class InstallControl : UserControl
    {
        public ObservableCollection<String> Messages;
        public InstallControl()
        {
            Messages = new ObservableCollection<string>();
            InitializeComponent();
            this.DataContext = Messages;
        }

        internal void LogThis(InstallManager.Msi.MsiMessageEventArgs e)
        {
            string text = String.Format("  {1}", e.MessageType, e.Message);
            tbMessage.Text = text;
            tbLog.AppendText(text + Environment.NewLine);
            Messages.Add(text);
        }
        internal void LogThis(string msg)
        {
            string text = String.Format(msg);
            tbMessage.Text = text;
            tbLog.AppendText(msg + Environment.NewLine);
            Messages.Add(text);
        }
        internal void InstallFinished()
        {
            progressBar.Maximum = 1;
            progressBar.Value = 1;
            progressBar.IsIndeterminate = false;

        }
        internal void InstallStarting()
        {
            Messages.Clear();
        }
        internal void OnInstallMessage(InstallManager.Msi.MsiMessageEventArgs e)
        {
            switch (e.MessageType)
            {
                case InstallManager.Msi.MsiInstallMessage.Progress:
                    ProcessProgressMessage(e.Message);
                    break;
                case InstallManager.Msi.MsiInstallMessage.ActionStart:
                    ProcessActionStartMessage(e.Message);
                    break;
                case InstallManager.Msi.MsiInstallMessage.CommonData:
                    Process_CommonData_Message(e.Message);
                    break;
                case InstallManager.Msi.MsiInstallMessage.Error:
                case InstallManager.Msi.MsiInstallMessage.FatalExit:
                case InstallManager.Msi.MsiInstallMessage.FilesInUse:
                case InstallManager.Msi.MsiInstallMessage.Info:
                case InstallManager.Msi.MsiInstallMessage.Initialize:
                case InstallManager.Msi.MsiInstallMessage.OutOfDiskSpace:
                case InstallManager.Msi.MsiInstallMessage.ResolveSource:
                case InstallManager.Msi.MsiInstallMessage.ShowDialog:
                case InstallManager.Msi.MsiInstallMessage.Terminate:
                case InstallManager.Msi.MsiInstallMessage.User:
                case InstallManager.Msi.MsiInstallMessage.Warning:
                case InstallManager.Msi.MsiInstallMessage.ActionData:
                default:
                    LogThis(e);
                    break;
            }


        }

        private void Process_CommonData_Message(string p)
        {
           
            var msg =MsiCommonDataMessage.Parse(p);
            LogThis("Common Data --" + msg.ToString());
        }

        private void ProcessActionStartMessage(string message)
        {
            LogThis("Action -------" +new MsiActionMessage(message).ToString());
        }

        private void ProcessProgressMessage(string message)
        {
            //string result = "Progress..." + message;
            //LogThis(result);
            MsiProgressMessage pm = new MsiProgressMessage(message);

          
            LogThis("Progress -----" + pm.ToString());

            return;
           
        }
    }
}
